package com.laikan.reader.bookshelf.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.laikan.library.components.BasicApplication;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.utils.AndroidUtils;
import com.laikan.reader.R;
import com.laikan.reader.bookshelf.fragment.BookShelfFragment;
import com.laikan.reader.bookstore.activity.BookSearchActivity;
import com.laikan.reader.core.events.LaiKanEvent;
import com.laikan.reader.core.interfaces.IResponseActivityEvent;
import com.socks.library.KLog;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

/**
 * @author Administrator
 * @category 书架页面
 */
public class BookShelfActivity extends BasicActivity {

    private ImageView iv_search, mIv_Del_Title;
    private long mkeyTime;
    public RelativeLayout mRL_Tab;
    private View popupView;
    private PopupWindow window;

    public boolean isShow = false;
    private RelativeLayout rl_pop;
    private TextView tv_look_history;
    private TextView tv_edit_shelf;
    private LaiKanEvent eventBus;
    private View rl_pop_window;

    @Override
    protected void assignViews() {
        KLog.e("BookShelf assignView start");
        setContentView(R.layout.lk_book_shelf_activity);

        iv_search = (ImageView)findViewById(R.id.iv_search);
        rl_pop = (RelativeLayout)findViewById(R.id.rl_pop);
        mIv_Del_Title = (ImageView)findViewById(R.id.iv__back);
        mRL_Tab = (RelativeLayout)findViewById(R.id.rl_tab);
        popupView = View.inflate(this, R.layout.popup_edit_shelf, null);
        tv_edit_shelf = (TextView)popupView.findViewById(R.id.tv_edit_shelf);
        tv_look_history = (TextView)popupView.findViewById(R.id.tv_look_history);
        View rl_pop_show_bg = popupView.findViewById(R.id.rl_pop_show_bg);
        rl_pop_window = popupView.findViewById(R.id.rl_pop);
        rl_pop_show_bg.setOnClickListener(this);
        tv_look_history.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        openEventBus();
        eventBus = LaiKanEvent.getInstance();
        //添加书架fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        BookShelfFragment fragment = new BookShelfFragment();
        fragmentTransaction.add(R.id.rl_view_group, fragment);
        fragmentTransaction.commit();
    }

    @Override
    protected void setListener() {
        iv_search.setOnClickListener(this);
        mIv_Del_Title.setOnClickListener(this);
        rl_pop.setOnClickListener(this);
        tv_edit_shelf.setOnClickListener(this);
    }

    @Override
    protected void onClickEvent(View view) {
        super.onClickEvent(view);
        switch (view.getId()) {
            case R.id.iv_search:
                Intent intent = new Intent();
                intent.setClass(mContext, BookSearchActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.rl_pop:
                showPopWindow();
                break;
            case R.id.tv_look_history: //查看历史
                MobclickAgent.onEvent(mContext, "sj-sy-cd-ls");//书架-首页-菜单-浏览历史
                window.dismiss();
                startActivity(new Intent(BookShelfActivity.this, ReadHistoryActivity.class));
                break;
            case R.id.tv_edit_shelf: //编辑书架
                window.dismiss();
                isShow = false;
                MobclickAgent.onEvent(mContext, "sj-sy-cd-bj");//书架-首页-菜单-编辑书架
                eventBus.setEventID(LaiKanEvent.EVENT_ID_BOOKSHELF_DELETE_MANAGER);
                BasicApplication.postEvent(eventBus);
                break;
            case R.id.rl_pop_show_bg:
                window.dismiss();
                isShow = false;
                break;
            default:
                break;
        }
    }

    private void showPopWindow() {
        MobclickAgent.onEvent(mContext, "sj-sy-cd");//书架-首页-菜单
        if (isShow) { // 已显示状态
            if (window != null) {
                window.dismiss();

            }
            isShow = false;
            return;
        }
        if (!isShow) {    // 不显示状态
            isShow = true;
        }
        window = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setOutsideTouchable(true);

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)rl_pop_window.getLayoutParams();

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            layoutParams.setMargins(0, AndroidUtils.dip2px(mContext,42),0,0);

        }else{
            layoutParams.setMargins(0, AndroidUtils.dip2px(mContext,42)+AndroidUtils.getTitleBarHeight(mContext),0,0);  //加上状态栏
        }
        rl_pop_window.setLayoutParams(layoutParams);
        window.showAtLocation(getCurrentFocus(), 0, 0, 0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        isShow = false;
        if (window != null && window.isShowing()) {
            window.dismiss();
            window = null;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        isShow = false;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //编辑书架时 屏蔽返回键
        if (mRL_Tab.getVisibility() != View.VISIBLE) {
            eventBus.setEventID(LaiKanEvent.EVENT_ID_BOOKSHELF_HIDE_EDIT); //隐藏编辑界面
            BasicApplication.postEvent(eventBus);
            return true;
        }
        // 2秒内连续按两次 退出程序
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mkeyTime) > 2000) {
                mkeyTime = System.currentTimeMillis();
                Toast.makeText(this, R.string.personal_exit_program, Toast.LENGTH_SHORT).show();
            }else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        List<Fragment> FragmetList = getSupportFragmentManager().getFragments();
        for (Fragment fragment : FragmetList) {
            if (fragment instanceof IResponseActivityEvent && fragment != null) {
                ((IResponseActivityEvent)fragment).onActivityResultEvent(requestCode, resultCode, data);
            }
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        return super.onKeyUp(keyCode, event);
    }
}